#!/usr/bin/env python
# encoding: utf-8
'''
@author: 王震
@license: 四川凡狼科技有限公司
@contact: 2668985108@qq.com
@file: curriculum.py
@time: 2021/10/1 8:44 上午
@desc:
'''
import json

from app.model.curriculum.curriculum import NewCourse
from app.msg.tips import tips, tipsText
from app.msg.resp import response_json, response_list_json
from main import db
from app.public.tool import create_key
from app.serialize.newCourseList import NewCourseList
from app.public.sqlMethods import get_obj, existence
from app.public.sqlMethods import auto_commit_db


def create_curriculum(_json):
    obj = {'name': ''}
    if existence(obj=_json, target=obj):
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    if get_obj(name=_json['name'], class_obj=NewCourse):
        return response_json(code=tips['existence'], msg=tipsText['existence'] % _json['name'])
    new_course = NewCourse()
    for key in _json:
        setattr(new_course, key, _json[key]) if hasattr(new_course, key) and _json[key] else None
    new_course.id = create_key()
    with auto_commit_db():
        db.session.add(new_course)
    return response_json()


def get_show_data(page_number, page_size, show):
    with auto_commit_db():
        pages = NewCourse.query.filter(NewCourse.show == int(show)).paginate(int(page_number), int(page_size), False)
        item = pages.items
        new_course = NewCourseList(many=True)
        data = new_course.dump(item)
    return response_list_json(data=data, page_number=pages.page, page_size=pages.pages)


def get_curriculum(page_number, page_size):
    with auto_commit_db():
        pages = NewCourse.query.paginate(int(page_number), int(page_size), False)
        item = pages.items
        new_course = NewCourseList(many=True)
        data = new_course.dump(item)
    return response_list_json(data=data, page_number=pages.page, page_size=pages.pages)


def update_curriculum(data):
    _id = data['id'] if 'id' in data else None
    name = data['name'] if 'name' in data else None
    if not _id or not name:
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    with auto_commit_db():
        res = NewCourse.query.filter(NewCourse.id == _id)
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
        _a = {}
        _course = NewCourse()
        for key in data:
            if hasattr(_course, key) and data[key]:
                _a[key] = data[key]
        res.update(_a)
    return response_json()


def delete_curriculum(_id):
    if not _id:
        return response_json(code=tips['parameter_error'], msg=tipsText['parameter_error'])
    with auto_commit_db():
        res = NewCourse.query.filter(NewCourse.id == _id).first()
        if not res:
            return response_json(code=tips['noResult'], msg=tipsText['noResult'])
        if res.show == 1:
            return response_json(code=tips['have_no_right'], msg=tipsText['have_no_right'])
        res.delete()
    return response_json()


def get_customer(page_number, page_size):
    page = NewCourse.query.filter(NewCourse.show == 1).paginate(int(page_number), int(page_size), False)
    item = page.items
    new_course = NewCourseList(many=True)
    data = new_course.dump(item)
    return response_list_json(data=data, page_number=page.page, page_size=page.pages)
